package com.qf.web.servlet;

import com.qf.pojo.CartDto;
import com.qf.pojo.TbUser;
import com.qf.service.CartService;
import com.qf.service.impl.CartServiceImpl;
import com.qf.utils.StringConstans;
import com.qf.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Create by 周志豪 on 2022/8/18 20:45
 *
 * @authoe 周志豪
 * @veision 1.0
 * //TODO  想好了再去做
 * @since 1.0
 */

public class CartServlet {

    private CartService cartService = new CartServiceImpl();


    public String create(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            return StringConstans.REDIRECT + "/index.jsp";
        }
        long goodId = Long.parseLong(id);

        TbUser user = (TbUser) request.getSession().getAttribute("user");

        Long userId = user.getId();
        cartService.upsert(goodId, userId);

        return StringConstans.REDIRECT + "/cartSuccess.jsp";
    }

    public String show(HttpServletRequest request, HttpServletResponse response) {

        TbUser user = (TbUser) request.getSession().getAttribute("user");
        Long userId = user.getId();
        CartDto cartDto = cartService.findCartGoodsByUserId(userId);
        request.getSession().setAttribute("cart",cartDto);
        return StringConstans.REDIRECT + "/cart.jsp";
    }

}
